package de.davvid.core;

import java.util.Calendar;
import javax.microedition.lcdui.DateField;
import java.util.Date;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Display;

import de.davvid.calendar.RecordMenu;
import de.davvid.contracts.*;

public class TimeRecordsMIDlet extends MIDlet {
	Display display;
	
	public Settings settings;
	public final ContractDatabase contractDB;
	
	public TimeRecordsMIDlet() {
		display = Display.getDisplay(this);
		settings = new Settings();
		contractDB = new ContractDatabase();
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		settings = null;
	}

	protected void pauseApp() {
		//Save Settings
		settings.save();
	}

	protected void startApp() throws MIDletStateChangeException {
		// Hier gehts los =)
		showMainMenu();
	}
	
	public void showMainMenu() {
		MainMenu mainMenu= new MainMenu(this);
		display.setCurrent(mainMenu);
		mainMenu = null;
	}

	public void showRecordMenu() {
		RecordMenu recordMenu = new RecordMenu(this);
		display.setCurrent(recordMenu);		
		recordMenu = null;
	}
	
	public void showContractList() {
		ContractListMenu contractListMenu = new ContractListMenu(this);
		display.setCurrent(contractListMenu);		
		contractListMenu = null;
	}	
	
	public void showCreateContractMenu() {
		CreateContractMenu createContractMenu = new CreateContractMenu(this);
		display.setCurrent(createContractMenu);		
		createContractMenu = null;
	}
	
	public void showDeleteContractMenu() {
		DeleteContractMenu deleteContractMenu = new DeleteContractMenu(this);
		display.setCurrent(deleteContractMenu);		
		deleteContractMenu = null;
	}
	
	public void showSettingsMenu() {
		SettingsMenu settingsMenu = new SettingsMenu(this);
		display.setCurrent(settingsMenu);
		settingsMenu = null;
	}
	
	public void showInfo() {
		InfoMenu infoMenu = new InfoMenu(this);
		display.setCurrent(infoMenu);
		infoMenu=null;
	}
	
	public void quit() {
		try {
			destroyApp(false);
			notifyDestroyed();
		} catch (MIDletStateChangeException e) {
		}
	}

	public static String dateToString (Date date,int mode)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		int day = c.get(Calendar.DATE);
		int month = c.get(Calendar.MONTH) + 1;
		int year = c.get(Calendar.YEAR);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		
		String t = "";
		
		if (mode == DateField.DATE || mode == DateField.DATE_TIME)
			t += (day<10?"0": "")+day+"-"+(month<10? "0": "")+month+"-"+(year<10? "0": "")+year+" ";	
		if (mode == DateField.TIME || mode == DateField.DATE_TIME)
			t += (hour<10? "0": "")+hour+":"+(minute<10? "0": "")+minute;
		
		return t;
	}
}
